package me.xingyannian.hope.config;

import cn.dev33.satoken.stp.StpInterface;
import me.xingyannian.hope.entity.Menu;
import me.xingyannian.hope.entity.Role;
import me.xingyannian.hope.entity.User;
import me.xingyannian.hope.service.MenuService;
import me.xingyannian.hope.service.UserService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

@Component
public class StpInterfaceImpl implements StpInterface {

    private UserService userService;
    private MenuService menuService;



    public StpInterfaceImpl(UserService userService, MenuService menuService) {
        this.userService = userService;
        this.menuService = menuService;
    }

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        Set<String> permissions = new HashSet<>();
        //System.out.println(loginId);
        User user = this.userService.getUserByLoginId((String) loginId);
        var roles = user.getRoles();
        System.out.println(roles.size());
        for (Role role: roles) {
            var menus = role.getMenus();
            for (Menu menu: menus) {
                System.out.println(menu.getPermission());
                permissions.add(menu.getPermission());
            }
        }
        return new ArrayList<>(permissions);
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roleNames = new ArrayList<>();
        System.out.println((String)loginId);
        User user = this.userService.getUserByLoginId((String) loginId);
        var roles = user.getRoles();
        for (Role role: roles) {
            roleNames.add(role.getName());
        }
        //System.out.println(roleNames.toString());
        return roleNames;
    }
}
